package com.aglhz.nature.view;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;



import org.json.JSONException;
import org.json.JSONObject;

import com.aglhz.nature.R;
import com.aglhz.nature.events.RegisterViewEvent;
import com.aglhz.nature.modle.CodeBean;
import com.aglhz.nature.utils.AsyncHttpCilentUtil;
import com.aglhz.nature.utils.CheckNumberThread;
import com.aglhz.nature.utils.ToastUtil;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{
	private static String TAG = RegisterActivity.class.getName();
	private EditText et_register_number;
	private EditText et_register_password;
	private EditText et_second_password;
	private EditText et_register_check_number;
	private Button sure_register;
	private CheckBox checkBox_remeberPassword;
	private ImageButton back;
	private TextView register_read;
	private CodeBean codeBean;
	private Button register_get_number;
	private int code =200;
	private SharedPreferences sharedPreferences;
	 private SharedPreferences.Editor editor;  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		EventBus.getDefault().register(this);
		et_register_number=(EditText) findViewById(R.id.et_register_number);
		et_register_password=(EditText) findViewById(R.id.et_register_password);
		et_second_password=(EditText) findViewById(R.id.et_second_password);
		et_register_check_number=(EditText)findViewById(R.id.et_register_check_number);
		checkBox_remeberPassword=(CheckBox)findViewById(R.id.checkBox_remeberPassword);
		register_read=(TextView)findViewById(R.id.register_read);
		back=(ImageButton) findViewById(R.id.login_back);
		register_get_number=(Button)findViewById(R.id.register_get_number);
		sure_register=(Button) findViewById(R.id.sure_register);
		
		
		register_read.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent =new Intent(RegisterActivity.this,ReadTextActivity.class);
				startActivity(intent);
			}
		});
		
		register_get_number.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				getCheckNumber();
			}
			
		});
		
		sure_register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				register();	
				
			}
			
		});
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});

	}
	
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventRegisterThread(RegisterViewEvent event) {

		String msg = event.getMessage();
		// update UI By event
		if ( RegisterViewEvent.EVENT_SHOWE_CODE == msg) {
			// update grid view
			this.showCode();
		} 
		

	}
	
	
	private void getCheckNumber(){
		
		if (et_register_number.getText().toString().length()!=11) {
			ToastUtil.showShort(RegisterActivity.this, "请输入正确手机号码");
			return;
		}
		//    
		AsyncHttpClient client = AsyncHttpCilentUtil.getInstance(RegisterActivity.this);
		RequestParams params=new RequestParams();
		params.add("phone", et_register_number.getText().toString());
		params.add("type", "v_regPhone");
		client.post("http://h5.aglhz.com/gc_community/client/validCode.do", params, new TextHttpResponseHandler(){

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				ToastUtil.show(RegisterActivity.this, "获取验证码失败");
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				Log.i(TAG, responseString);
				try {
					JSONObject data =new JSONObject(responseString).getJSONObject("other");
					if (data.getInt("code")==200) {
						 ToastUtil.show(RegisterActivity.this, "验证码已发送，请留意短信");
						 register_get_number.setEnabled(false);
						 register_get_number.setText("60s后再获取");
						Thread t= new Thread(new CheckNumberThread(register_get_number));
						t.start();
					}else {
						ToastUtil.showShort(RegisterActivity.this, "获取验证码失败");
					}
					
				} catch (JSONException e) {
					ToastUtil.showShort(RegisterActivity.this, "获取验证码异常");
					e.printStackTrace();
				}
			}
			
		});
	}
	
	
	
	private void showCode() {
		int a = Integer.parseInt(codeBean.getOther().getCode());
		if(a==code){
			Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
			 sharedPreferences = getSharedPreferences("Regist_INFO",Context.MODE_PRIVATE);  
			 editor = sharedPreferences.edit();  
			 editor.putString("RegistNumber",et_register_number.getText().toString().trim());
			 editor.putString("RegistPassword",et_register_password.getText().toString().trim());
			 editor.commit();  
			
			 Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
			startActivity(intent);
			finish();
		}else {
			Toast.makeText(RegisterActivity.this, "该账号已被注册", Toast.LENGTH_LONG).show();
		}
	
	}

	//http://h5.aglhz.com/client/register2.do?account=123&Password1=qqqqqq&Password2=qqqqqq
private void register() {
	
	if (et_register_check_number.getText().toString().length()!=6) {
		ToastUtil.showShort(RegisterActivity.this, "请输入正确验证码");
		return;
	}
	if (!et_register_password.getText().toString().equals(et_second_password.getText().toString())) {
		ToastUtil.showShort(RegisterActivity.this, "输入密码不一致，请重新输入");
		return;
	}
	
	if (!checkBox_remeberPassword.isChecked()) {
		ToastUtil.showShort(RegisterActivity.this, "请先阅读注册协议并同意");
		return;
	}
	
	//http://h5.aglhz.com/gc_community/client/register.do
// String url="http://h5.aglhz.com/gc_community/client/register2.do?account="+et_register_number.getText().toString().trim()+"&Password1="+et_register_password.getText().toString().trim()+"&Password2="+et_second_password.getText().toString().trim();
	AsyncHttpClient client = AsyncHttpCilentUtil.getInstance(RegisterActivity.this);
	RequestParams params=new RequestParams();
	params.add("account", et_register_number.getText().toString());
	params.add("code", et_register_check_number.getText().toString());
	params.add("Password1", et_register_password.getText().toString());
	params.add("Password2", et_second_password.getText().toString());
	
	client.post("http://h5.aglhz.com/gc_community/client/register.do", params ,new TextHttpResponseHandler() {
		
		@Override
		public void onSuccess(int statusCode,
				cz.msebera.android.httpclient.Header[] headers,
				String responseString) {
			 try {
				
				 Gson gson = new Gson();
				 CodeBean otBean = gson.fromJson(responseString,CodeBean.class);
				 codeBean= (CodeBean) otBean.clone();
				 // send event
				 RegisterViewEvent event = new   RegisterViewEvent(
						 RegisterViewEvent.EVENT_SHOWE_CODE);
				 EventBus.getDefault().post(event);
				 
				 } catch (Exception e) {
				 e.printStackTrace();
				 }
			
			
		}
		
		@Override
		public void onFailure(int statusCode,
				cz.msebera.android.httpclient.Header[] headers,
				String responseString, Throwable throwable) {
			
			 Log.e(TAG, "failure");
		}
	




	});

	
}

@Override
protected void onDestroy() {
	EventBus.getDefault().unregister(this);
	super.onDestroy();
}
}
